package org.farm2.tools.web.dto;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class SendMsgDto {
    //指令消息
    private String msg;
    //当前引用内容
    private String quotingText;
    //是否检索知识库
    private String searchKnow;
    //会话id
    private String talkKey;
    /**
     * //消息模式
     * | "SEARCH_ANALYSIS" //智能检索
     * | "CREAT_ASSISTANT" //创作助手
     * | "QUESTION_ANSWERING" //知识问答
     * | "FORM_COMPLETION"; //表单完善
     */
    private String msgType;


    //系统返回的消息
    private String sysMsg = "";
    //历史消息id
    private List<String> hisTalkKeys;
    //历史消息
    private List<SendMsgDto> hisTalks = new ArrayList<>();
    //参考资料
    private List<QuotingKnowDto> quotingKnows = new ArrayList<>();


    public String getMsgType() {
        return msgType;
    }

    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }

    public List<QuotingKnowDto> getQuotingKnows() {
        return quotingKnows;
    }

    public void setQuotingKnows(List<QuotingKnowDto> quotingKnows) {
        this.quotingKnows = quotingKnows;
    }

    public List<SendMsgDto> getHisTalks() {
        return hisTalks;
    }

    public void setHisTalks(List<SendMsgDto> hisTalks) {
        this.hisTalks = hisTalks;
    }

    public List<String> getHisTalkKeys() {
        return hisTalkKeys;
    }

    public void setHisTalkKeys(List<String> hisTalkKeys) {
        this.hisTalkKeys = hisTalkKeys;
    }

    public String getSysMsg() {
        return sysMsg;
    }

    public void setSysMsg(String sysMsg) {
        this.sysMsg = sysMsg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getQuotingText() {
        return quotingText;
    }

    public void setQuotingText(String quotingText) {
        this.quotingText = quotingText;
    }

    public String getTalkKey() {
        return talkKey;
    }

    public void setTalkKey(String talkKey) {
        this.talkKey = talkKey;
    }

    public String getSearchKnow() {
        return searchKnow;
    }

    public void setSearchKnow(String searchKnow) {
        this.searchKnow = searchKnow;
    }

    public String getFormatMsg(int tokenSize) {
        if ((tokenSize - 100) > 0) {
            tokenSize = tokenSize - 100;
        }
        boolean hasQuoting = false;
        String formatMsg = "我的问题或指令是{{{" + msg + "}}}";
        if (StringUtils.isNotBlank(quotingText)) {
            hasQuoting = true;
            if ("QUESTION_ANSWERING".equals(msgType)) {
                formatMsg = formatMsg + ",请针如下内容{{{" + quotingText + "}}}";
            } else {
                formatMsg = formatMsg + ",知识库有这些资料可以参考{{{" + quotingText + "}}}";
            }
            if (formatMsg.length() > tokenSize) {
                return returnByOverLength(formatMsg, tokenSize);
            }
        }
        if (this.getQuotingKnows().size() > 0) {
            hasQuoting = true;
            formatMsg = formatMsg + ",知识库有这些资料可以参考{{{";
            for (QuotingKnowDto know : getQuotingKnows()) {
                formatMsg = formatMsg + "{{" + know.getText() + "}}";
                if (formatMsg.length() > tokenSize) {
                    return returnByOverLength(formatMsg, tokenSize);
                }
            }
            formatMsg = formatMsg + "}}}";
        }
        {//处理历史消息，超长的裁掉
            int tempsize = tokenSize - formatMsg.length();
            List<SendMsgDto> lessHisMsg = new ArrayList<>();
            for (SendMsgDto his : hisTalks) {
                if (his == null) {
                    continue;
                }
                int hisSize = (his.getMsg() + his.getSysMsg()).length();
                tempsize = tempsize - hisSize;
                if (tempsize > 0) {
                    lessHisMsg.add(his);
                } else {
                    break;
                }
            }
            this.hisTalks = lessHisMsg;
        }
        if (!hasQuoting) {
            formatMsg = msg;
        }
        return formatMsg;
    }

    /**
     * 超长时返回消息(需要清空历史消息)
     *
     * @param formatMsg
     * @return
     */
    private String returnByOverLength(String formatMsg, int size) {
        this.hisTalks.clear();
        this.hisTalkKeys.clear();
        return formatMsg.substring(0, size - 1);
    }
}
